<template>
  <div class="about-master-container">
    <base-breadcrumb :breadcrumb="!isAudit">
      <b-steps
        :current="stepVal"
        direction="vertical"
        @change.self="stepChange"
      >
        <b-step title="基本信息">
          <a-card
            v-show="activeKey[0]"
            slot="description"
            v-loading="loading"
            :bordered="false"
          >
            <a-form-model
              ref="ruleForm"
              :model="formData"
              :rules="rules"
            >
              <a-row>
                <a-col span="8">
                  <a-form-model-item
                    label="物资招标采购申请编码"
                    prop="purchaseApplyCode"
                  >
                    <a-input-search
                      v-model="formData.purchaseApplyCode"
                      placeholder="请选择"
                      :disabled="state === 3"
                      @search="onSearch"
                      @click="onSearch"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item label="编号">
                    <BaseInput
                      v-model="formData.purchaseApplyNumber"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item label="物资招标文件评审编码">
                    <BaseInput
                      v-model="formData.fileCode"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
              </a-row>
              <a-row>
                <a-col span="8">
                  <a-form-model-item
                    label="工程名称"
                    prop="projectId"
                  >
                    <a-select
                      v-model="formData.projectId"
                      :disabled="state === 3"
                    >
                      <a-select-option
                        v-for="item in getProgrammeArr"
                        :key="item.id"
                        :value="item.id"
                      >
                        {{ item.name }}
                      </a-select-option>
                    </a-select>
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item
                    label="招标文件编号"
                    prop="fileNumber"
                  >
                    <BaseInput
                      v-model="formData.fileNumber"
                      :disabled="state === 3"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item
                    label="招标类别"
                    prop="tenderTypeId"
                  >
                    <a-select
                      v-model="formData.tenderTypeId"
                      :disabled="state === 3"
                    >
                      <a-select-option
                        v-for="item in optionArr"
                        :key="item.id"
                        :value="item.id"
                      >
                        {{ item.name }}
                      </a-select-option>
                    </a-select>
                  </a-form-model-item>
                </a-col>
              </a-row>
              <a-row>
                <a-col span="8">
                  <a-form-model-item label="发包人结算单价合计">
                    <BaseInput
                      v-model="formData.sumSettleDiff"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item label="发包人结算金额合计">
                    <BaseInput
                      v-model="formData.sumSettle"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item label="招标控制单价合计">
                    <BaseInput
                      v-model="formData.sumTotalDiff"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
              </a-row>
              <a-row>
                <a-col span="8">
                  <a-form-model-item label="招标控制金额合计">
                    <BaseInput
                      v-model="formData.sumTotal"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item label="单价差合计">
                    <BaseInput
                      v-model="formData.sumUnitDiff"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item label="金额差合计">
                    <BaseInput
                      v-model="formData.sumUnit"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
              </a-row>
              <a-row>
                <a-col span="8">
                  <a-form-model-item label="创建人">
                    <BaseInput
                      v-model="formData.creatorName"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item label="创建时间">
                    <BaseInput
                      v-model="formData.createTime"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item label="评审文件上传">
                    <l-table
                      :is-close="state!==3"
                      business-code="MM12"
                      :count="1"
                      :upload-arrys.sync="formData.fileList"
                    />
                    <!-- <l-table
                      :is-close="state!==3"
                      :upload-arrys="formData.fileList"
                      business-code="MM12"
                      @fileArrysData="fileArrysData"
                    /> -->
                  </a-form-model-item>
                </a-col>
              </a-row>
              <a-row>
                <a-col span="24">
                  <a-form-model-item
                    label="评审内容"
                    prop="comments"
                  >
                    <editor
                      ref="editor"
                      v-model="formData.comments"
                      :max-length="50000"
                      :is-clear="true"
                      :disabled="state === 3"
                      @change="commentsVal"
                    />
                  </a-form-model-item>
                </a-col>
              </a-row>
            </a-form-model>
            <div
              v-if="!isAudit"
              class="pub-next-btn"
            >
              <a-button
                v-if="state !== 3"
                type="primary"
                @click="temporaryStorage()"
              >
                保存草稿
              </a-button>
              <a-button
                type="primary"
                @click="temporaryStorage(true)"
              >
                下一步
              </a-button>
            </div>
          </a-card>
        </b-step>
        <b-step title="物资明细">
          <a-card
            v-if="activeKey[1]"
            slot="description"
            :bordered="false"
          >
            <materials
              :id="formData.id"
              ref="materials"
              :state="state"
              :apply-id="formData.purchaseApplyId"
              @getTotal="getEditInfo"
            />
            <div
              v-if="!isAudit"
              class="pub-next-btn"
            >
              <a-button
                type="primary"
                @click="previous"
              >
                上一步
              </a-button>
              <a-button
                type="primary"
                @click="next"
              >
                下一步
              </a-button>
            </div>
          </a-card>
        </b-step>
        <!--                <b-step title="评审意见表">-->
        <!--                    <a-card :bordered="false" slot="description" v-if="activeKey[2]">-->
        <!--                        <opinion :state="state" :id="row ? formData.id : null" ref="opinion"></opinion>-->
        <!--                        <div class="pub-next-btn" v-if="!isAudit">-->
        <!--                            <a-button type="primary" @click="previous">上一步</a-button>-->
        <!--                            <a-button type="primary" @click="next">下一步</a-button>-->
        <!--                        </div>-->
        <!--                    </a-card>-->
        <!--                </b-step>-->
        <b-step title="相关附件">
          <a-card
            v-if="activeKey[2]"
            slot="description"
            :bordered="false"
          >
            <u-table
              :upload-list="addFile"
              :delete-url="fileDel"
              :is-show="state !== 3"
              :list-url="fileList"
              :business-id="formData.id"
              business-code="MM03"
            />
            <div
              v-if="!isAudit"
              class="pub-next-btn"
            >
              <a-button
                type="primary"
                @click="previous"
              >
                上一步
              </a-button>
            </div>
          </a-card>
        </b-step>
      </b-steps>
      <div
        v-if="!isAudit"
        class="pub-header-btn"
      >
        <template v-if="state !== 3">
          <a-button
            :loading="loading"
            type="primary"
            @click="submit"
          >
            提交
          </a-button>
        </template>
        <BasePrintButton
          v-if="formData.id && state == 3"
          :before-print-fn="beforePrintFn"
          style="display: inline-block"
        />
        <a-button
          v-checkVisibleInDetail
          @click="$close"
        >
          关闭
        </a-button>
      </div>
      <apply-list
        ref="applyList"
        @getRow="getRow"
      />
    </base-breadcrumb>
  </div>
</template>

<script>
import materials from '../components/materials/materials'
import applyList from '../components/applyList/applyList'
import {
  reviewEditBasic, reviewAddBasic, audit,
  addFile, fileList, fileDel, reviewBasicDetail,
  print
} from '@/api/material/procurement/review'
import { selectOption } from '@/api/material/goods/goods'
import editor from '@/components/Editor/editor'

export default {
  name: 'a' + Date.now(),
  components: {
    materials,
    applyList,
    editor
  },
  props: {
    isAudit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      beforePrintFn: async () => {
        let res = await print({ id: this.formData.id })
        // res.data = res.data.replaceAll('\n', '\\')
        return res.data.content
      },
      activeKey: [true, false, false, false, false, false, false],
      state: Number(this.$route.query.state),
      isSubmit: false,
      loading: false,
      row: this.$route.query.row,
      formData: {
        createTime: this.getNowTime(),
        creatorName: JSON.parse(sessionStorage.getItem('ycloud-user_info')).name,
        fileList: []
      },
      optionArr: [],
      number: null,
      content: '',
      stepVal: 0,
      isNext: false,
      rules: {
        fileReviewFileId: { required: true, message: '请上传文件', trigger: 'change' },
        purchaseApplyCode: { required: true, message: '请输入编号', trigger: 'change' },
        projectId: { required: true, message: '请选择工程名称', trigger: 'change' },
        fileNumber: { required: true, message: '请输入招标文件编号', trigger: 'blur' },
        tenderTypeId: { required: true, message: '请选择招标类别', trigger: 'change' },
        comments: { required: true, message: '请输入评审内容', trigger: 'blur' }
      },
      addFile: addFile(),
      fileDel: fileDel(),
      fileList: fileList(),
      getProgrammeArr: []
    }
  },
  watch: {
    isAudit: {
      immediate: true,
      handler(val) {
        if (val) {
          this.state = 3
        }
      }
    },
    stepVal(newVal, oldVal) {
      this.$set(this.activeKey, newVal, true)
    }

  },
  mounted() {
    this.getOption()
    this.getProgramme()
    if (this.state !== 1) {
      this.formData.id = this.$route.query.id
      this.getEditInfo()
      this.isSubmit = true
    }
  },
  methods: {
    previous() {
      this.isNext = true
      this.stepVal--
      if (this.stepVal < 0) {
        this.stepVal = 0
      }
    },
    getProgramme() {
      selectOption({
        type: 18
      }).then(res => {
        if (res.code === 200) {
          this.getProgrammeArr = res.data
        }
      })
    },
    commentsVal(val) {
      this.formData.comments = val
    },
    fileArrysData(val) {
      return
      this.formData.fileReviewFileId = val[0].commonId
      this.formData.fileInfo = val[0]
      console.log(val)
    },
    next(val) {
      this.isNext = true
      this.stepVal++
    },
    stepChange(val) {
      if (!this.formData.id && !this.isAudit) {
        this.$message.warning('切换步骤前请先填写基本信息')
        this.stepVal = 0
      } else {
        if (!this.isNext) {
          this.$set(this.activeKey, val, !this.activeKey[val])
        }
      }
      this.isNext = false
    },

    onSearch(val) {
      this.$refs.applyList.open()
    },
    getRow(val) {
      console.log(val)
      this.formData = {
        ...this.formData,
        purchaseApplyId: val.id,
        purchaseApplyCode: val.code,
        purchaseApplyNumber: val.number
      }
    },
    getOption() {
      selectOption({ type: 5 }).then(res => {
        if (res.code === 200) {
          this.optionArr = res.data
        }
      })
    },
    // 获取编辑回显数据
    async getEditInfo() {
      this.loading = true
      let res = await reviewBasicDetail({ id: this.formData.id })
      if (res.code === 200) {
        this.formData = { ...this.formData, ...res.data }
        this.formData.fileList = this.formData.fileInfo ? [this.formData.fileInfo] : []
      }
      this.$console(this.formData)
      this.loading = false
    },
    submit() {
      // this.save('approve');
      if (this.isSubmit) {
        this.$submitConfirm(() => {
          this.loading = true
          audit({
            baseId: this.formData.id,
            auditStatus: this.formData.auditStatus
          }).then(res => {
            this.loading = false
            this.$message.success('提交成功')
            this.$router.push({ path: '/material/procurement/review' })
          })
        })
      } else {
        this.$message.warning('提交之前请先暂存')
      }
    },
    temporaryStorage(isNext) {
      if (this.state === 3) {
        this.next()
        return
      }
      this.$refs.ruleForm.validate(async valid => {
        if (valid) {
          let text = this.$refs.editor.editor.txt.text()
          if (!text) {
            return this.$message.warning('请输入评审内容')
          }
          if (this.formData.fileList.length) {
            this.formData.fileReviewFileId = this.formData.fileList[0].commonId
            this.formData.fileInfo = this.formData.fileList[0]
          } else {
            this.formData.fileReviewFileId = ''
            this.formData.fileInfo = {}
          }

          if (!this.formData.fileReviewFileId) {
            return this.$message.warning('请上传评审文件')
          }
          if (this.state === 2 || this.formData.id !== undefined) {
            let res = await reviewEditBasic(this.formData)
            if (res.code === 200) {
              this.$message.success('暂存成功')
              this.isSubmit = true
              if (isNext) {
                this.next()
              }
            }
          } else {
            let res = await reviewAddBasic(this.formData)
            if (res.code === 200) {
              this.$message.success('暂存成功')
              this.isSubmit = true
              if (isNext) {
                this.next()
              }
              this.formData.id = res.data.id
              await this.getEditInfo()
            }
          }
        } else {
          this.$message.warning("请填写完所有必填项信息")
          return false
        }
      })
    }
  }
}
</script>
